<?php
namespace Hawk\Controller;
use \Hawk\Controller\IndexController;
class PartController extends IndexController 
{
	/**
     * 部门列表
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function lst(){
		$pmodel = D('Hawk/Part');
		$where=true;
		$list = $pmodel->getPart($where);
		$this->assign('list',$list);
		$this->display();
	}
	/**
     * 添加部门
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function add(){
		if(IS_POST){
			$pmodel = D('Hawk/Part');
			if($pmodel->create(I('post.'), 1))
    		{
    			if($id = $pmodel->add())
    			{
                    //写入操作日志
                    $content = "成功添加部门".$_POST['name'];
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
    				$this->success('添加成功！', U('lst?p='.I('get.p')));
    				exit;
    			}else{
                    //写入操作日志
                    $content = "添加部门".$_POST['name']."失败";
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
                }
    		}
    		$this->error($model->getError());
		}else{
			$model=M('');
			$pmodel = D('Hawk/Part');
			$data = $model
			->table(C('DB_PREFIX').'part')
			->select();
			$list = $pmodel->reSort($data);
			$this->assign('list',$list);
			$this->display();
		}
	}
	/**
     * 修改部门
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function edit(){
		if(IS_POST)
    	{
    		$model = D('Hawk/Part');
    		if($model->create(I('post.'), 2))
    		{
    			if($model->save() !== FALSE)
    			{
                    //写入操作日志
                    $content = "成功修改部门".$_POST['name'];
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);

    				$this->success('修改成功！', U('lst', array('p' => I('get.p', 1))));
    				exit;
    			}else{
                    //写入操作日志
                    $content = "修改部门".$_POST['name']."失败";
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
                }
    		}
    		$this->error($model->getError());
    	}else{
    		$model=M('');
			$pmodel = D('Hawk/Part');
			$self = $model
			->table(C('DB_PREFIX').'part')
			->find(I('get.id'));
			$this->assign('self',$self);
			$data = $model
			->table(C('DB_PREFIX').'part')
			->select();
			$list = $pmodel->reSort($data);
			$this->assign('list',$list);
			$this->display();
    	}
	}
	/**
     * 删除部门
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function delete(){
		
	}
	/**
     * 部门排序
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function sort(){
		if(IS_POST){
			$model=M('');
			$data['id'] = $_POST['part_id'];
			$data['sort'] = $_POST['sort'];
			$re = $model
			->table(C('DB_PREFIX').'part')
			->data($data)
			->save();
			if($re){
				echo json_encode(array('code'=>1,'msg'=>'排序成功！请刷新页面'));
				exit;
			}else{
				echo json_encode(array('code'=>1,'msg'=>'排序失败！'));
			}
		}else{
			echo json_encode(array('code'=>1000,'msg'=>'无此权限！'));
		}
	}
	/**
     * 禁用部门
     * @author hawk <2271030162@qq.com>
     * @return mix
     */
	public function disabled(){
		if(IS_POST){
			$id = $_POST['id'];
			$model=M('');
			$count = $model
			->table(C('DB_PREFIX').'admin')
			->where(array('part_id='.$id))
			->count();
			if($count>0){
				echo json_encode(array('code'=>0,'msg'=>'该部门下还有员工不可禁用！！'));
				exit;
			}else{
				$data['id']=$id;
				if($_POST['is_use']==1){
					$data['is_use']=0;
					$code=2;
					$msg='禁用成功！';
				}else{
					$data['is_use']=1;
					$code=1;
					$msg='恢复成功！';
				}
				$re = $model
				->table(C('DB_PREFIX').'part')
				->data($data)
				->save();
				if($re){
					$content = "部门（".$id."）".$msg;
                    hawk_operation_log(session('id'),hawk_get_ip(),$content);
					echo json_encode(array('code'=>$code,'msg'=>$msg));
					exit;
				}else{
					echo json_encode(array('code'=>0,'msg'=>'操作失败！'));
					exit;
				}
			}
		}else{
			echo json_encode(array('code'=>1000,'msg'=>'无此权限'));
			exit;
		}
	}
}